package com.cloud.api.user.service;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.stereotype.Service;

import com.cloud.api.user.repository.PlayUserClientESRepository;
import com.cloud.cache.common.RedisCacheKeys;
import com.cloud.cache.service.RedisService;
import com.cloud.common.core.BMDataContext;
import com.cloud.common.exception.CloudException;
import com.cloud.common.util.UKTools;
import com.cloud.model.PlayUserClient;

import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;

@Service
@CacheConfig(cacheNames = "user")
public class UserService {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	private ReactiveRedisTemplate<String, String> reactiveRedisTemplate;

	@Resource
	private RedisService<PlayUserClient> redisService;

	@Autowired
	private PlayUserClientESRepository playUserClientESRepository;

	/**
	 * 注册一个匿名用户
	 *
	 * @param userId
	 * @throws CloudException
	 */
//	@CachePut(value="user",key="#id")
	public Mono<PlayUserClient> register(String userId) throws CloudException {
		try {
			Hooks.onOperatorDebug();
			PlayUserClient playUserClient = new PlayUserClient();
			playUserClient
					.setUsername("Guest_" + Base64.encodeBase64String(UKTools.getUUID().toLowerCase().getBytes()));
			playUserClient.setPassword("");
			playUserClient.setPlayertype(BMDataContext.PlayerTypeEnum.NORMAL.toString());
			playUserClient.setCreatetime(new Date());
			playUserClient.setUpdatetime(new Date());
			playUserClient.setLastlogintime(new Date());
			playUserClient.setOrgi(BMDataContext.SYSTEM_ORGI);
			playUserClient.setGoldcoins(0);
			playUserClient.setCards(0);
			playUserClient.setDiamonds(0);
			playUserClient.setGamestatus("READY");
			// playUserClient.setId(userId);
//			redisService.save(RedisCacheKeys.USER_KEY_HEAD + playUserClient.getId(), playUserClient);
			return playUserClientESRepository.save(playUserClient);
		} catch (Exception e) {
			logger.error("注册游客失败！", e);
			throw new CloudException(-1, "注册游客失败！");
		}
	}

//	@Cacheable(value = "user", key = RedisCacheKeys.USER_KEY_HEAD + "#id", unless = "#result==null")
	public Mono<PlayUserClient> findByIdInCache(String id) {
		return redisService.get(RedisCacheKeys.USER_KEY_HEAD + id, PlayUserClient.class)
				.switchIfEmpty(playUserClientESRepository.findById(id).flatMap(myData -> {
					return redisService.save(RedisCacheKeys.USER_KEY_HEAD + id, myData).map(saveResult -> {
						return myData;
					});
				}));

	}
	
	
//	@Cacheable(value = "user", key = "#id")
//	public Mono<PlayUserClient> findByIdInCache(String id) {
//		return playUserClientESRepository.findById(id);
//	}

}
